﻿using FarsiLibrary.Win;
using System.Windows.Forms;
using System;
using Nuwanda.TfsSpotlight.Shell.Proxies.Interfaces;

namespace Nuwanda.TfsSpotlight.Shell.Proxies
{
    /// <summary>A custom tab strip item to introduce interface</summary>
    public class TabStripItem : FATabStripItem, ITabStripItem
    {
        public const string MODIFIED_TAB_CONTENTS_MARK = "*";

        private readonly ControlInvoker _tabInvoker;

        private Control _view;
        public Control View
        {
            get { return this._view; }
            set
            {
                this._tabInvoker.RunInControlsThread(
                    delegate
                        {
                            this.Controls.Clear();
                            this.Controls.Add(value);
                            this._view = value;
                            this._view.Dock = DockStyle.Fill;
                        });
            }
        }

        public ITabPresenter Presenter { get; set; }

        public TabStripItem()
        {
            this._tabInvoker = new ControlInvoker(this);
        }

        public void SetCaption(string caption)
        {
            this._tabInvoker.RunInControlsThread(() => this.Title = caption);
        }

        public void RefreshCaption()
        {
            var dirtyMark = this.Presenter.IsDirty ? MODIFIED_TAB_CONTENTS_MARK : String.Empty;
            var locked = this.Locked ? "(locked)" : String.Empty;
            this.SetCaption(locked + this.Presenter.GetCaption() + dirtyMark);
        }

        private bool _locked;
        public bool Locked
        {
            get { return _locked; }
            set
            {
                this._tabInvoker.RunInControlsThread(() =>
                        {
                            _locked = value;
                            this.Enabled = value == false;
                            RefreshCaption();
                        });
            }
        }
    }
}